package com.tenbit.emp.ctl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.emp.svc.EMP1000SVC;
import com.tenbit.emp.vo.EMP1000VO;
import com.tenbit.emp.vo.EMP1010VO;
import com.tobesoft.platform.PlatformRequest;
import com.tobesoft.platform.PlatformResponse;
import com.tobesoft.platform.data.ColumnInfo;
import com.tobesoft.platform.data.Dataset;
import com.tobesoft.platform.data.DatasetList;
import com.tobesoft.platform.data.VariableList;

import emp.tenbit.emp.bean.SearchEmpBean;
import emp.tenbit.emp.bean.TestBean;

@Controller
public class EMP1000CTL {
	@Autowired
	private EMP1000SVC emp1000svc;
	private Logger log=Logger.getLogger(this.getClass());
	
	@RequestMapping("/doSearchEmpView.do")
	public void doSearchEmpView(HttpServletRequest request, HttpServletResponse response){
		
		log.debug( "=======end: "+ this.getClass().getName()+ "."+ new Exception().getStackTrace()[0].getMethodName()
		           + "=======");

		PlatformRequest pReq = null;
		
		try {
			pReq = new PlatformRequest(request, "utf-8");
			pReq.receiveData();
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		VariableList in_vl = new VariableList();
		VariableList out_vl = new VariableList();
		DatasetList  dl = new DatasetList();
		Map<String, Object> paraMap = new HashMap<String, Object>();
		
		in_vl = pReq.getVariableList();
		
		String employeeName = in_vl.getValueAsString("employeeName");
		
		Dataset ds = new Dataset("output");
		ds.addColumn("employeeId",ColumnInfo.CY_COLINFO_STRING, 20);
		ds.addColumn("employeeName",ColumnInfo.CY_COLINFO_STRING, 20);
		ds.addColumn("deptName",ColumnInfo.CY_COLINFO_STRING, 20);
		
		//TODO: paging
		String page = "1";
		
		List<EMP1010VO> list =emp1000svc.getEmpSearchList(employeeName);
		
		for (EMP1010VO emp : list) {
			int row = ds.appendRow();
				
				ds.setColumn(row, "employeeId", emp.getEmployee_id() );
				ds.setColumn(row, "employeeName", emp.getName() );
				ds.setColumn(row, "deptName", emp.getDept_code() );
		}
		
		dl.addDataset(ds);
		
		
		//out.clearBuffer();
		PlatformResponse pRes;
		try {
			pRes = new PlatformResponse(response, PlatformRequest.XML, "utf-8");
			pRes.sendData(out_vl, dl);
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		log.debug( "=======end: "+ this.getClass().getName()+ "."+ new Exception().getStackTrace()[0].getMethodName()
		           + "=======");

		
	}
	@RequestMapping("/test.do")
	public String test(TestBean bean,HttpServletRequest req){
		
		/*Iterator<Entry<String, String[]>> it=req.getParameterMap().entrySet().iterator();
		
		while(it.hasNext()){
			Entry<String, String[]> entry=it.next();
			
			log.debug("test "+entry.getKey());
			
			for (String s : entry.getValue()) {
				log.debug(s);
			}
			
		}*/
		
		Enumeration<String> e=req.getParameterNames();
		while (e.hasMoreElements()) {
			log.debug("test1 "+e.nextElement());
		}
		
		return "test2";
	}
	public ModelAndView doInitMain(){
		ModelAndView mv=new ModelAndView();
		List<EMP1000VO> sex_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> dept_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> skill_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> academic_code=new ArrayList<EMP1000VO>();
		
		mv.setViewName("EMP1000M0");
		mv.addObject("listZip", emp1000svc.getCodeZipByMvList());
		
		for (EMP1000VO zip : emp1000svc.getCodeZipByMvList()) {
			if(zip.getCode_type().equals("SEX")){
				sex_code.add(zip);
			}else if(zip.getCode_type().equals("DEPT")){
				dept_code.add(zip);
			}if(zip.getCode_type().equals("SKILL")){
				skill_code.add(zip);
			}else if(zip.getCode_type().equals("ACADEMIC")){
				academic_code.add(zip);
			}
		}
		
		mv.addObject("sex_code", sex_code);
		mv.addObject("dept_code", dept_code);
		mv.addObject("skill_code", skill_code);
		mv.addObject("academic_code", academic_code);
		
		
		return mv;
	}
	@RequestMapping("/empMainView.do")
	public ModelAndView doViewMain(){
		return doInitMain();
	}
	@RequestMapping("/empInsertView.do")
	public ModelAndView doViewIns(){
		ModelAndView mv=doInitMain();
		
		mv.setViewName("EMP1010P0");
		
		return mv;
	}
	@RequestMapping("/listEmp.do")
	public ModelAndView getEmpList(SearchEmpBean searchBean,@RequestParam(value="page",required=false) String page){
		log.debug("==========================>getEmpList "+searchBean);
		ModelAndView mv=doInitMain();
		
		if(searchBean.getName()!= null){	
			mv.addObject("viewBean", emp1000svc.doMakeView(searchBean));
		}
		mv.setViewName("EMP1000M0");
		mv.addObject("viewTable","yes");
		
		return emp1000svc.getEmpList(searchBean,page,mv);
	}
	@RequestMapping("/empDelOrModView.do")
	public ModelAndView doViewDelOrMod(@RequestParam("emp_id") String empId){
		
		ModelAndView mv=doInitMain();
		
		emp1000svc.getDelOrModList(mv,empId);
		mv.setViewName("EMP1020P0");
		mv.addObject("employee_id", empId);
		
		return mv;
	}
}
